#include "NetAction.h"
#include <QCoreApplication>
#include <QNetworkInterface>
#include <QHostAddress>
#include <QDebug>
#include <random>

#include "FileCtrl.h"
std::string nameFilePath = "D:/git/gitee/trans/TransQt/Common/English_Cn_Name_Corpus.txt";

QString NetAction::terminalName = NetAction::getTempName();
QString NetAction::ip = NetAction::getIPAddress();

NetAction::NetAction() {}


QString NetAction::getIPAddress()
{
    QString ipAddr = nullptr;
    QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
    for (const QNetworkInterface &interface : interfaces)
    {
        if (interface.flags() & QNetworkInterface::IsUp && !(interface.flags() & QNetworkInterface::IsLoopBack))
        {
            QList<QHostAddress> addresses = interface.allAddresses();
            for (const QHostAddress &address : addresses)
            {
                // 只获得ipv4 且不是网关
                if (address.isNull())
                {
                    continue;
                }
                if(address.protocol() == QAbstractSocket::IPv4Protocol && !ifNetgate(address.toString().toStdString())){
                    ipAddr = address.toString();
                }
            }
        }
    }
    return ipAddr;
}

//判断是否为网关，最后的数字一般为1
bool NetAction::ifNetgate(const string& ip){
    string::size_type  index = ip.find_last_of(".");
    if(index == string::npos){
        return false;
    }
    string lastNumStr = ip.substr(index+1);
    if( 0 == lastNumStr.compare("1")){
        return true;
    }
    return false;
}


QString NetAction::getTempName()
{
    UINT32 num = getRandomNum();
    QDir currentDir = QDir(QCoreApplication::applicationDirPath());
    QDEBUG_FILE_FUNC<< currentDir.absolutePath();
    
    QString name = FileCtrl::readLineOfNameFile(nameFilePath,num);
    if(name.isEmpty()){
        name = QString("default");
    }
    return name;
}
string NetAction::getTcpPort()
{
    return "";
}
QString NetAction::getBroadcastAddresses()
{
    QString ipAddr = "255.255.255.255";
    QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
    for (const QNetworkInterface &interface : interfaces)
    {
        if (interface.flags() & QNetworkInterface::IsUp && !(interface.flags() & QNetworkInterface::IsLoopBack))
        {
            QList<QNetworkAddressEntry> addresses = interface.addressEntries();

            for (const QNetworkAddressEntry &entry : addresses)
            {
                if(entry.ip().toString() == NetAction::ip){
                    QDEBUG_FILE_FUNC<<"braocast address is :"<<entry.broadcast().toString();
                    return entry.broadcast().toString();
                }
            }
        }
    }
    return ipAddr;
}

UINT32 NetAction::getRandomNum(){
     // 获取当前时间点
    auto now = std::chrono::system_clock::now();
    
    // 将时间点转换为自epoch以来的毫秒数
    auto duration = now.time_since_epoch();
    auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
    
    // 使用毫秒数作为种子

    std::default_random_engine generator(static_cast<unsigned int>(milliseconds));
    
    // 定义一个随机数分布，例如均匀分布在1到100之间
    std::uniform_int_distribution<UINT32> distribution(1, 40000);
    
    // 生成随机数
    UINT32 random_number = distribution(generator);
    return random_number;
}


